package com.station.Activitys.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.jaredrummler.materialspinner.MaterialSpinner;
import com.seek.biscuit.Biscuit;
import com.seek.biscuit.CompressResult;
import com.seek.biscuit.OnCompressCompletedListener;
import com.station.Activitys.R;
import com.station.Activitys.activity.base.BaseActivity;
import com.station.Activitys.adapter.ImageDisplayMagAdapter;
import com.station.Activitys.adapter.ImageMagAdapter;
import com.station.Activitys.bean.ImageJson;
import com.station.Activitys.bean.ImageMag;
import com.station.Activitys.bean.State;
import com.station.Activitys.bean.Survey;
import com.station.Activitys.bean.SurveyDetails;
import com.station.Activitys.http.manager.WorkManager;
import com.station.Activitys.utils.CacheManger;
import com.station.Activitys.utils.Constant;
import com.station.Activitys.utils.DialogUtils;
import com.station.Activitys.utils.StringUtils;
import com.station.Activitys.utils.ToastUtils;
import com.zhihu.matisse.Matisse;
import com.zhy.autolayout.AutoLinearLayout;

import org.simple.eventbus.EventBus;
import org.simple.eventbus.Subscriber;
import org.simple.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * 项目名称：基站管理平台 手机app
 * 类描述：基站规划 ---> 申请可研
 * 创建人：张飞祥
 * 创建时间：2018/8/30 9:37
 * 修改人：张飞祥
 * 修改时间：2018/8/30 9:37
 * 修改备注：
 */
public class PlanSubmitActivity extends BaseActivity implements MaterialSpinner.OnItemSelectedListener, CompoundButton.OnCheckedChangeListener{

    //TODO 标题
    //返回按钮
    @Bind(R.id.iv_return) ImageView ivReturn;
    //标题
    @Bind(R.id.tv_title) TextView tvTitle;

    //TODO 基站 基础信息
    //基站名称
    @Bind(R.id.tv_name) TextView tvName;
    //基站状态
    @Bind(R.id.tv_state) TextView tvState;
    //省份
    @Bind(R.id.tv_province) TextView tvProvince;
    //城市
    @Bind(R.id.tv_city) TextView tvCity;
    //网格
    @Bind(R.id.tv_grid) TextView tvGrid;
    //厂家
    @Bind(R.id.sp_vender) MaterialSpinner spVender;
    //物理站址名称
    @Bind(R.id.tv_physical_name) TextView tvPhysicalName;
    //物理站址地址
    @Bind(R.id.tv_physical_address) TextView tvPhysicalAddress;
    //设计院名称
    @Bind(R.id.tv_ui_yard) TextView tvUiYard;
    //区域类型
    @Bind(R.id.sp_area_type) MaterialSpinner spAreaType;
    //主场景类型
    @Bind(R.id.sp_main_scene_type) MaterialSpinner spMainSceneType;
    //站型
    @Bind(R.id.tv_station_model) TextView tvStationModel;
    //站址经度
    @Bind(R.id.tv_gps_x) TextView tvGpsX;
    //站址维度
    @Bind(R.id.tv_gps_y) TextView tvGpsY;
    //优先级
    @Bind(R.id.sp_priority) MaterialSpinner spPriority;
    //站址所属
    @Bind(R.id.et_territoriality) EditText etTerritoriality;
    //工程期属
    @Bind(R.id.sp_project_num) MaterialSpinner spProjectNum;

    //TODO 规划信息
    //总布局
    @Bind(R.id.layout_survey_msg) AutoLinearLayout layoutSurveyMsg;
    //TODO 公用信息
    //自动获取的经纬度
    @Bind(R.id.tv_gps) TextView tvGps;
    //手动输入的经度
    @Bind(R.id.et_gps_x) EditText etGpsX;
    //手动输入的纬度
    @Bind(R.id.et_gps_y) EditText etGpsY;
    //信号强度
    @Bind(R.id.tv_signal) TextView tvSignal;
    //反填图片列表
    @Bind(R.id.rv_http_img_list) RecyclerView rvHttpImgList;
    //新增图片列表
    @Bind(R.id.rv_img_list) RecyclerView rvImgList;

    //TODO 室外站
    //非室分总布局
    @Bind(R.id.layout_not_room) AutoLinearLayout layoutNotRoom;
    //天线挂高
    @Bind(R.id.et_antenna_height) EditText etAntennaHeight;
    //天线方位角
    @Bind(R.id.et_antenna_horn) EditText etAntennaHorn;
    //是否BBU集中部署
    @Bind(R.id.cb_bbu) CheckBox cbBbu;
    //扇区配置
    @Bind(R.id.sp_sector) MaterialSpinner spSector;
    //RRU规划功率
    @Bind(R.id.sp_rru_plan) MaterialSpinner spRruPlan;
    //RRU手法需求
    @Bind(R.id.sp_rru_receive) MaterialSpinner spRruReceive;
    //是否远程电下倾调整
    @Bind(R.id.cb_adjust) CheckBox cbAdjust;

    //TODO 室内站
    //室分总布局
    @Bind(R.id.layout_room) AutoLinearLayout layoutRoom;
    //室分子场景类型
    @Bind(R.id.sp_scene_type) MaterialSpinner spSceneType;
    //频段
    @Bind(R.id.sp_frequency_channel) MaterialSpinner spFrequencyChannel;
    //室分类型
    @Bind(R.id.sp_type) MaterialSpinner spType;
    //覆盖建筑、楼层或位置
    @Bind(R.id.et_cover_land) EditText etCoverLand;
    //覆盖面积
    @Bind(R.id.et_cover_acreage) EditText etCoverAcreage;
    //有源室分的PRRU信源个数
    @Bind(R.id.et_yes_prru) EditText etYesPrru;
    //无源室分的PRRU信源个数
    @Bind(R.id.et_no_pru) TextView etNoPru;
    //无源室分单双路类型
    @Bind(R.id.sp_no_pru_type) MaterialSpinner spNoPruType;

    /**
     * 变量
     */
    //初始化网络请求
    public WorkManager mApiManager;
    //TODO 实体类
    //基站规划 实体类
    private Survey mSurvey = null;
    //用户 id
    private int mUserId;

    //TODO 上传图片 变量
    //数据源
    private List<ImageMag> mImageList = new ArrayList<>();
    //图片反填数据适配器
    private ImageDisplayMagAdapter mHttpAdapter = null;
    //上传图片适配器
    private ImageMagAdapter mUploadAdapter = null;
    //当前 那个操作上传图片 的 pos
    private int mPos;

    //TODO 下拉列表
    //厂家
    private List<String> mVenderList = null;
    private String mVender = "";
    //区域类型
    private List<String> mAreaTypeList = null;
    private String mAreaType = "";
    //主场景类型
    private List<String> mMainSceneTypeList = null;
    private String mMainSceneType = "";
    //工程期属
    private List<String> mProjectNumList = null;
    private String mProjectNum = "";
    //优先级
    private List<String> mPriorityList = null;
    private String mPriority = "";
    //扇区配置
    private List<String> mSectorList = null;
    private String mSector = "";
    //RRU规划功率
    private List<String> mRruPlanList = null;
    private String mRruPlan = "";
    //RRU收发需求
    private List<String> mRruReceiveList = null;
    private String mRruReceive = "";
    //室分子场景类型
    private List<String> mSceneTypeList = null;
    private String mSceneType = "";
    //频段
    private List<String> mFrequentList = null;
    private String mFrequent = "";
    //室分类型
    private List<String> mTypeList = null;
    private String mType = "";
    //无源室分单双路类型
    private List<String> mSinglePairTypeList = null;
    private String mSinglePairType = "";

    //TODO  字符串
    //是否bbu集中部署
    private String mBBUFlag = "";
    //是否远程电下倾调整
    private String mAdjustFlag = "";

    @Override
    protected void initUI() {
        setContentView(R.layout.act_plan_submit);
        //网络请求接口 常驻方法
        mApiManager = mApiManager.getWorkApiManager(this, this.getTaskId());
    }

    @Override
    protected void initData() {

        //获取从上个页面 传递过来的 实体类
        Bundle bundle = this.getIntent().getExtras();
        //获取 规划 实体类
        mSurvey = (Survey) bundle.getSerializable(Constant.INTENT_SURVEY_BEAN);

        //获取 用户 id
        mUserId = CacheManger.getUserId(mACache);

        //解决 滑动冲突
        rvHttpImgList.setNestedScrollingEnabled(false);
        rvImgList.setNestedScrollingEnabled(false);

        //设置标题
        tvTitle.setText("申请可研");
        //显示返回按钮
        ivReturn.setVisibility(View.VISIBLE);

        //TODO 填充基础数据
        setBasicsData();
        //TODO 选择事件（全部）
        setItemOnClickAll();
        //TODO 开始网络请求获取 数据
        initShowTimeDialog("正在加载数据，请稍后....", this);
        mApiManager.workDetails(mSurvey.getId());
    }

    private void setItemOnClickAll() {

        //TODO 下拉列表
        //厂家
        spVender.setOnItemSelectedListener(this);
        //区域类型
        spAreaType.setOnItemSelectedListener(this);
        //主场景类型
        spMainSceneType.setOnItemSelectedListener(this);
        //优先级
        spPriority.setOnItemSelectedListener(this);
        //工程期属
        spProjectNum.setOnItemSelectedListener(this);
        //扇区配置
        spSector.setOnItemSelectedListener(this);
        //RRU规划功率
        spRruPlan.setOnItemSelectedListener(this);
        //RRU手法需求
        spRruReceive.setOnItemSelectedListener(this);
        //室分子场景类型
        spSceneType.setOnItemSelectedListener(this);
        //频段
        spFrequencyChannel.setOnItemSelectedListener(this);
        //室分类型
        spType.setOnItemSelectedListener(this);
        //无源室分单双路类型
        spNoPruType.setOnItemSelectedListener(this);

        //TODO 单选事件
        //是否远程电下倾调整
        cbAdjust.setOnCheckedChangeListener(this);
        //是否BBU集中部署
        cbBbu.setOnCheckedChangeListener(this);

    }

    /**
     * 填充基础数据
     */
    private void setBasicsData() {
        /**
         * 填充 数据
         */
        if (mSurvey != null) {
            //基站名称
            tvName.setText(mSurvey.getPhysicalStationName());
            //基站状态
            tvState.setText(mSurvey.getState());
            //省份
            tvProvince.setText(mSurvey.getProvince());
            //城市
            tvCity.setText(mSurvey.getLocalNetworkAddress());
            //网格
            tvGrid.setText(mSurvey.getGrid());
            //物理站址名称
            tvPhysicalName.setText(mSurvey.getPhysicalStationName());
            //物理站址地址
            tvPhysicalAddress.setText(mSurvey.getAddressOfPhysicalStation());
            //站型
            tvStationModel.setText(mSurvey.getStationModel());
            //站址经度
            tvGpsX.setText(mSurvey.getGPS_X());
            //站址维度
            tvGpsY.setText(mSurvey.getGPS_Y());
            //站址所属
            etTerritoriality.setText(mSurvey.getHighSpeedRailwayStation());

            //TODO 从内存中 获取 当前列表集合 并填充到列表中
            //厂家
            mVenderList = CacheManger.getSpinnerKey(mACache, Constant.CACHE_MANUFACTOR);
            if (mVenderList!=null){
                spVender.setItems(mVenderList);
                for (int i = 0;i<mVenderList.size();i++){
                    String string = mVenderList.get(i);
                    if (string.equals(mSurvey.getVender())){
                        //设置 点击默认值
                        mVender = string;
                        //设置列表默认选中值
                        spVender.setSelectedIndex(i);
                    }
                }
            }
            //区域类型
            mAreaTypeList = CacheManger.getSpinnerKey(mACache, Constant.CACHE_REGION_TYPE);
            if (mAreaTypeList!=null){
                spAreaType.setItems(mAreaTypeList);
                for (int i = 0;i<mAreaTypeList.size();i++){
                    String string = mAreaTypeList.get(i);
                    if (string.equals(mSurvey.getAreaType())){
                        //设置 点击默认值
                        mAreaType = string;
                        //设置列表默认选中值
                        spAreaType.setSelectedIndex(i);
                    }
                }
            }
            //主场景类型
            mMainSceneTypeList = CacheManger.getSpinnerKey(mACache, Constant.CACHE_MAIN_TYPE);
            if (mMainSceneTypeList!=null){
                spMainSceneType.setItems(mMainSceneTypeList);
                for (int i = 0;i<mMainSceneTypeList.size();i++){
                    String string = mMainSceneTypeList.get(i);
                    if (string.equals(mSurvey.getMainSceneType())){
                        //设置 点击默认值
                        mMainSceneType = string;
                        //设置列表默认选中值
                        spMainSceneType.setSelectedIndex(i);
                    }
                }
            }
            //优先级
            mPriorityList = CacheManger.getSpinnerKey(mACache, Constant.CACHE_PRIORITY);
            if (mPriorityList!=null){
                spPriority.setItems(mPriorityList);
                for (int i = 0;i<mPriorityList.size();i++){
                    String string = mPriorityList.get(i);
                    if (string.equals(mSurvey.getPriority())){
                        //设置 点击默认值
                        mPriority = string;
                        //设置列表默认选中值
                        spPriority.setSelectedIndex(i);
                    }
                }
            }
            //工程期属
            mProjectNumList = CacheManger.getSpinnerKey(mACache, Constant.CACHE_PROJECT_NUMBER);
            if (mProjectNumList!=null){
                spProjectNum.setItems(mProjectNumList);
                //设置 点击默认值
                mProjectNum = mProjectNumList.get(0);
            }
        }
    }

    /**
     * 获取基站信息数据 报错
     */
    @Subscriber(tag = Constant.BUS_WORK_DETAILS_ERROR, mode = ThreadMode.MAIN)
    public void initHttpError(String httpError) {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                //销毁弹窗
                mTimeDialog.dismiss();
                //弹出提示
                ToastUtils.showShort(PlanSubmitActivity.this, Constant.NETWORK_ERROR_MSG);
            }
        }, 1000);
    }

    /**
     * 获取基站信息数据 报错
     */
    @Subscriber(tag = Constant.BUS_WORK_DETAILS, mode = ThreadMode.MAIN)
    public void initHttpData(final SurveyDetails details) {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                //销毁弹窗
                mTimeDialog.dismiss();

                //TODO 判断 是否 有规划详情，有则显示，否则 不显示
                if (details != null) {
                    //显示 规划详情View
                    layoutSurveyMsg.setVisibility(View.VISIBLE);

                    //站型
                    String model = details.getStationModel();
                    //开始填充数据
                    //TODO 基础数据
                    //设计院名称
                    tvUiYard.setText(details.getDesignInstitudeName());
                    //自动获取的经纬度
                    tvGps.setText("经度：" + details.getAutoGpsX() + "   纬度：" + details.getAutoGpsY());
                    //手动输入的经度
                    etGpsX.setText(details.getManualInputGpsX());
                    //手动输入的纬度
                    etGpsY.setText(details.getManualInputGpsY());
                    //信号强度
                    tvSignal.setText(details.getSignalIntensity() + " dBm");
                    //图片列表
                    mHttpAdapter = new ImageDisplayMagAdapter(PlanSubmitActivity.this, details.getImgs(),true);
                    //添加 适配器 样式
                    rvHttpImgList.setLayoutManager(new LinearLayoutManager(PlanSubmitActivity.this, LinearLayoutManager.VERTICAL, false));
                    //添加 适配器
                    rvHttpImgList.setAdapter(mHttpAdapter);

                    //TODO 逻辑站型数据
                    if (model.equals(Constant.STATION_TYPE_NO_ROOM))
                        initShowRoom(details);
                    else
                        initShowNotRoom(details);

                    //TODO  图片上传
                    mUploadAdapter = new ImageMagAdapter(PlanSubmitActivity.this, mImageList) {
                        @Override
                        public void setImageUploadPos(int position) {
                            mPos = position;
                        }
                    };
                    //添加 适配器 样式
                    rvImgList.setLayoutManager(new LinearLayoutManager(PlanSubmitActivity.this, LinearLayoutManager.VERTICAL, false));
                    //添加 适配器
                    rvImgList.setAdapter(mUploadAdapter);
                    //添加动画
                    rvImgList.setItemAnimator(new DefaultItemAnimator());
                    //解决 滑动冲突
                    rvImgList.setNestedScrollingEnabled(false);
                }
            }
        }, 1000);
    }

    /**
     * 室外站 数据填充
     */
    private void initShowNotRoom(SurveyDetails details) {
        //显示非室分页面
        layoutNotRoom.setVisibility(View.VISIBLE);
        //天线挂高
        etAntennaHeight.setText(details.getHeight());
        //天线方位角
        etAntennaHorn.setText(details.getAntennaAzimuth());
        //是否BBU集中部署
        mBBUFlag = details.getIsBBUCentralizedDeployment();
        if (mBBUFlag!=null && Constant.STRING_YES.equals(mBBUFlag)){
            cbBbu.setChecked(true);
        }else{
            cbBbu.setChecked(false);
            mBBUFlag = Constant.STRING_NO;
        }

        //扇区配置
        mSectorList = CacheManger.getSpinnerKey(mACache, Constant.CACHE_SECTOR);
        if (mSectorList != null) {
            spSector.setItems(mSectorList);
            for (int i = 0;i<mSectorList.size();i++){
                String string = mSectorList.get(i);
                if (string.equals(details.getSectorAllocation())){
                    //设置 点击默认值
                    mSector = string;
                    //设置列表默认选中值
                    spSector.setSelectedIndex(i);
                }
            }
        }

        //RRU规划功率
        mRruPlanList = CacheManger.getSpinnerKey(mACache, Constant.CACHE_RRU_PLAN);
        if (mRruPlanList != null) {
            spRruPlan.setItems(mRruPlanList);
            for (int i = 0;i<mRruPlanList.size();i++){
                String string = mRruPlanList.get(i);
                if (string.equals(details.getRRUPlanningPower())){
                    //设置 点击默认值
                    mRruPlan = string;
                    //设置列表默认选中值
                    spRruPlan.setSelectedIndex(i);
                }
            }
        }

        //RRU收发需求
        mRruReceiveList = CacheManger.getSpinnerKey(mACache, Constant.CACHE_RRU_RECEIVE);
        if (mRruReceiveList != null) {
            spRruReceive.setItems(mRruReceiveList);
            for (int i = 0;i<mRruReceiveList.size();i++){
                String string = mRruReceiveList.get(i);
                if (string.equals(details.getRRUTransceiverRequirements())){
                    //设置 点击默认值
                    mRruReceive = string;
                    //设置列表默认选中值
                    spRruReceive.setSelectedIndex(i);
                }
            }
        }

        //是否远程电下倾调整
        mAdjustFlag = details.getRemotePowerDownAdjustmentIsRequired();
        if (mAdjustFlag!=null && Constant.STRING_YES.equals(mAdjustFlag)){
            cbAdjust.setChecked(true);
        }else{
            cbAdjust.setChecked(false);
            mAdjustFlag = Constant.STRING_NO;
        }
    }

    /**
     * 室内站 数据填充
     */
    private void initShowRoom(SurveyDetails details) {
        //显示室分页面
        layoutRoom.setVisibility(View.VISIBLE);

        //覆盖建筑、楼层或位置
        etCoverLand.setText(details.getCoveringBuildingsFloorsOrLocations());
        //覆盖面积
        etCoverAcreage.setText(details.getCoverageArea());
        //有源室分的PRRU信源个数
        etYesPrru.setText(details.getPRRUSourcesNum());
        //无源室分的PRRU信源个数
        etNoPru.setText(details.getNumberOfRRUSourcesForPassiveCellDivision());

        //室分子场景类型
        mSceneTypeList = CacheManger.getSpinnerKey(mACache, Constant.CACHE_ROOM_SCENE_TYPE);
        if (mSceneTypeList != null) {
            spSceneType.setItems(mSceneTypeList);
            for (int i = 0;i<mSceneTypeList.size();i++){
                String string = mSceneTypeList.get(i);
                if (string.equals(details.getChamberMolecularSceneType())){
                    //设置 点击默认值
                    mSceneType = string;
                    //设置列表默认选中值
                    spSceneType.setSelectedIndex(i);
                }
            }
        }
        //频段
        mFrequentList = CacheManger.getSpinnerKey(mACache, Constant.CACHE_FREQUENT);
        if (mFrequentList != null) {
            spFrequencyChannel.setItems(mFrequentList);
            for (int i = 0;i<mFrequentList.size();i++){
                String string = mFrequentList.get(i);
                if (string.equals(details.getFrequencyChannel())){
                    //设置 点击默认值
                    mFrequent = string;
                    //设置列表默认选中值
                    spFrequencyChannel.setSelectedIndex(i);
                }
            }
        }
        //室分类型
        mTypeList = CacheManger.getSpinnerKey(mACache, Constant.CACHE_ROOM_TYPE);
        if (mTypeList != null) {
            spType.setItems(mTypeList);
            for (int i = 0;i<mTypeList.size();i++){
                String string = mTypeList.get(i);
                if (string.equals(details.getCompartmentType())){
                    //设置 点击默认值
                    mType = string;
                    //设置列表默认选中值
                    spType.setSelectedIndex(i);
                }
            }
        }
        //无源室分单双路类型
        mSinglePairTypeList = CacheManger.getSpinnerKey(mACache, Constant.CACHE_ROOM_SINGLE_PAIR_TYPE);
        if (mSinglePairTypeList != null) {
            spNoPruType.setItems(mSinglePairTypeList);
            for (int i = 0;i<mSinglePairTypeList.size();i++){
                String string = mSinglePairTypeList.get(i);
                if (string.equals(details.getPassiveCellTypeSingleAndDoubleChannelType())){
                    //设置 点击默认值
                    mSinglePairType = string;
                    //设置列表默认选中值
                    spNoPruType.setSelectedIndex(i);
                }
            }
        }

    }

    /**
     * 下拉列表 选择监听事件
     * @param view
     * @param position
     * @param id
     * @param item
     */
    @Override
    public void onItemSelected(MaterialSpinner view, int position, long id, Object item) {
        switch (view.getId()){

            case R.id.sp_vender:                    //厂家
                mVender = item.toString();
                break;

            case R.id.sp_area_type:                 //区域类型
                mAreaType = item.toString();
                break;

            case R.id.sp_main_scene_type:           //主场景类型
                mMainSceneType = item.toString();
                break;

            case R.id.sp_priority:                  //优先级
                mPriority = item.toString();
                break;

            case R.id.sp_project_num:               //工程期属
                mProjectNum = item.toString();
                break;

            case R.id.sp_sector:                    //扇区配置
                mSector = item.toString();
                break;

            case R.id.sp_rru_plan:                  //RRU规划功率
                mRruPlan = item.toString();
                break;

            case R.id.sp_rru_receive:               //RRU收发需求
                mRruReceive = item.toString();
                break;

            case R.id.sp_scene_type:                //室分场景类型
                mSceneType = item.toString();
                break;

            case R.id.sp_frequency_channel:         //频段
                mFrequent = item.toString();
                break;

            case R.id.sp_type:                      //室分类型
                mType = item.toString();
                break;

            case R.id.sp_no_pru_type:               //无源室分单双路类型
                mSinglePairType = item.toString();
                break;
        }
    }

    /**
     * 复选按钮 选中事件
     * @param btn
     * @param flag
     */
    @Override
    public void onCheckedChanged(CompoundButton btn, boolean flag) {
        switch (btn.getId()){
            case R.id.cb_bbu:           //是否BBU集中部署
                if (flag)
                    mBBUFlag = Constant.STRING_YES;
                else
                    mBBUFlag = Constant.STRING_NO;
                break;
            case R.id.cb_adjust:        //是否远程电下倾调整
                if (flag)
                    mAdjustFlag = Constant.STRING_YES;
                else
                    mAdjustFlag = Constant.STRING_NO;
                break;
        }
    }

    /**
     * 按钮点击事件
     * @return
     */
    @OnClick({R.id.iv_return,R.id.layout_add_list,R.id.btn_submit})
    public void onClick(View view){
        switch (view.getId()){

            case R.id.iv_return:         //返回
                finish();
                break;

            case R.id.layout_add_list:   //分组添加事件
                ImageMag imageMag = new ImageMag();
                imageMag.setName("");
                imageMag.setHttpImgList(new ArrayList<String>());
                imageMag.setLocalImgList(new ArrayList<String>());

                //上传图片Flag 初始化 （true：当前位置可以继续上传，false：当前位置不可以上传了）
                List<Boolean> mHttpImgFlag = new ArrayList<>();
                for(int i = 0;i < Constant.MAX_SELECT_PIC_NUM;i++){
                    mHttpImgFlag.add(i,true);
                }
                imageMag.setHttpImgFlag(mHttpImgFlag);
                //添加一个 item
                mUploadAdapter.addItem(mImageList.size(),imageMag);
                break;

            case R.id.btn_submit:         //提交按钮
                //站型
                String model = mSurvey.getStationModel();
                //站型 网络请求判断
                if (model.equals(Constant.STATION_TYPE_NO_ROOM))
                    setDataSubmit(false);
                else
                    setDataSubmit(true);
                break;
        }
    }


    /**
     * 数据提交
     * 1.roomFlag：false --> 非室分  true --> 室分
     */
    private void setDataSubmit(boolean roomFlag) {

        //站址所属
        String mTerritoriality = etTerritoriality.getText().toString();
        //手动获取的 经度
        String mMyGpsX = etGpsX.getText().toString();
        //手动获取的 纬度
        String mMyGpsY = etGpsY.getText().toString();

        //TODO 非室分
        //天线挂高
        String antennaHeight = etAntennaHeight.getText().toString();
        //天线方位角
        String antennaHorn = etAntennaHorn.getText().toString();

        //TODO 室分
        //覆盖建筑、楼层或位置
        String coverLand = etCoverLand.getText().toString();
        //覆盖面积
        String coverAcreage = etCoverAcreage.getText().toString();
        //有源室分的PRRU信源个数
        String yesPrru = etYesPrru.getText().toString();
        //无源室分的PRU信源个数
        String noRru = etNoPru.getText().toString();

        //图片上传 json
        List<ImageJson> list = mUploadAdapter.getUploadImgList();
        //添加 新上传图片的数据
        list.addAll(mHttpAdapter.getHttpImgList());
        //将 list 转换成 json 字符串
        String imgJson = new Gson().toJson(list).toString();

        //定义一个 检索 图片分组名是否为空的 flag
        boolean imgUploadFlag = false;
        for (ImageJson img : list){
            if (img.getImgCategorys().equals("")){
                imgUploadFlag = false;
                break;
            }else{
                imgUploadFlag = true;
            }
        }

        /**
         * 逻辑判断
         */
        if (list.size()!=0 && !imgUploadFlag){
            ToastUtils.showShort(this,"还有组名没有填写！");
        }else{
            //网络请求 提交
            initShowTimeDialog("正在提交申请可研，请稍后....", this);
            if (!roomFlag){
                //非室分
                mApiManager.workPlanApplyStudy(mUserId,mSurvey.getId(),mVender,mAreaType,
                        mMainSceneType,mPriority,mTerritoriality,imgJson,mMyGpsX,mMyGpsY,antennaHeight,
                        antennaHorn,mBBUFlag,mSector,mRruPlan,mRruReceive,mAdjustFlag,
                        "",
                        "",
                        "",
                        "",
                        "",
                        "",
                        "",
                        "",mProjectNum);
            }else{
                //室分
                mApiManager.workPlanApplyStudy(mUserId,mSurvey.getId(),mVender,mAreaType,
                        mMainSceneType,mPriority,mTerritoriality,imgJson,mMyGpsX,mMyGpsY,
                        "", "",
                        "","",
                        "","",
                        "",
                        mSceneType, mFrequent, mType, coverLand, coverAcreage, yesPrru, noRru,
                        mSinglePairType,mProjectNum);
            }
        }
    }


    /**
     * 工单 规划 ---> 申请可研 提交 解析报错
     */
    @Subscriber(tag = Constant.BUS_WORK_PLAN_APPLY_STUDY_ERROR, mode = ThreadMode.MAIN)
    public void initHttpSubmitError(String strError) {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                //隐藏dialog
                mTimeDialog.dismiss();
                //弹出相应的提示
                ToastUtils.showShort(PlanSubmitActivity.this,Constant.NETWORK_ERROR_MSG);
            }
        }, 1000);
    }

    /**
     * 工单 规划 ---> 申请可研 提交 数据返回成功
     */
    @Subscriber(tag = Constant.BUS_WORK_PLAN_APPLY_STUDY, mode = ThreadMode.MAIN)
    public void initHttpSubmitData(final State state) {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                //隐藏dialog
                mTimeDialog.dismiss();

                //TODO 网络请求业务逻辑判断
                if (state.getState().equals("1")) {
                    mMsgDialog = new DialogUtils() {
                        @Override
                        public void onNegativeClick() {
                        }
                        @Override
                        public void onPositiveClick() {
                            //通知刷新 基站规划 列表 的数据
                            EventBus.getDefault().post("WorkPush",Constant.BUS_WORK_PUSH);
                            //销毁当前页面
                            finish();
                        }
                    };
                    mMsgDialog.showMsgDialog(PlanSubmitActivity.this,"申请成功",
                            "当前规划信息已申请可研成功！","好的，知道了");
                }else{
                    ToastUtils.showShort(PlanSubmitActivity.this,state.getMsg());
                }

            }
        }, 1000);
    }



    /**
     * 图片处理返回页面
     *
     * @param requestCode
     * @param resultCode
     * @param data
     */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        //TODO 选择之后的 图片 url 返回
        if (requestCode == Constant.REQUEST_CAMERA_PHOTO) {
            /**
             * 1.是否已经点击确认了
             * 2.获取返回 的 url 集合
             * 3.开始进行 图片压缩
             * 4.根据压缩后的 图片路径 进行填充到 图片集合
             * 5.刷新 adapter 数据
             */
            if (resultCode == Activity.RESULT_OK) {
                //TODO 获取当前图片路径 转成 URi的形式
                List<String> mSelected = Matisse.obtainPathResult(data);
                //TODO 开始图片压缩
                Biscuit.with(this)
                        .path(mSelected)
                        .listener(new OnCompressCompletedListener() { //压缩监听
                            @Override
                            public void onCompressCompleted(CompressResult compressResult) {
                                //TODO 本地图片
                                //获取图片 返回 的压缩 后的路径
                                List<String> urls = compressResult.mSuccessPaths;
                                //获取现在 真正的 数据集合
                                mImageList = mUploadAdapter.getImageMags();
                                //添加到图片集合里面去
                                mImageList.get(mPos).getLocalImgList().addAll(urls);
                                //图片选择回调 列表刷新
                                mUploadAdapter.notifyDataSetChanged();

                                //TODO 网络图片
                                //获取集合
                                List<String> httpImg = mImageList.get(mPos).getAdapter().getHttpUrls();
                                //填充到数据结合中
                                List<String> imgs = mImageList.get(mPos).getHttpImgList();
                                imgs.addAll(httpImg);
                                imgs = StringUtils.removeDuplicate(imgs);

                                mImageList.get(mPos).getHttpImgList().clear();
                                mImageList.get(mPos).getHttpImgList().addAll(imgs);
                            }
                        })
                        .ignoreLessThan(100)//忽略小于100kb的图片不压缩，返回原图路径
                        .build()            //开始执行
                        .asyncCompress();   //异步压缩
            }
        }
    }

}
